package com.baidumap.administrator.happystudy.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.adapter.ShoppingPayAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车
 */
public class Fragment3 extends Fragment implements View.OnClickListener {

    private View view;
    private ListView lvBuy;
    private ImageView ivChoose;
    private LinearLayout llChooseAll;
    private TextView tvPay;
    private TextView tvMoney;
    private ShoppingPayAdapter adapter = null;
    private List<Boolean> mList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fg3, container, false);
        initView(view);
        for (int i = 0; i < 6; i++) {
            mList.add(false);
        }
        return view;
    }


    private void initView(View view) {
        lvBuy = (ListView) view.findViewById(R.id.lv_buy);
        ivChoose = (ImageView) view.findViewById(R.id.iv_choose);
        llChooseAll = (LinearLayout) view.findViewById(R.id.ll_choose_all);
        tvPay = (TextView) view.findViewById(R.id.tv_pay);
        tvMoney = (TextView) view.findViewById(R.id.tv_money);

        llChooseAll.setOnClickListener(this);
        tvPay.setOnClickListener(this);
        lvBuy.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("---", "onItemClick: ---" + position);
            }
        });
        adapter = new ShoppingPayAdapter(getActivity(), mList);
        adapter.setOnSeletedItem(new ShoppingPayAdapter.SeletedListener() {
            @Override
            public void onSeletedItem(boolean isSelected) {
                if (!isSelected && llChooseAll.isSelected()) {
                    llChooseAll.setSelected(false);
                }
            }

            @Override
            public void onSeletedAllItem(boolean isSelected) {
                if (isSelected) {
                    llChooseAll.setSelected(true);
                } else {
                    llChooseAll.setSelected(false);
                }
            }
        });
        lvBuy.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_choose_all://全选
                if (ivChoose.isSelected()) {
                    ivChoose.setSelected(false);
                    int size = mList.size();
                    mList.clear();
                    for (int i = 0; i < size; i++) {
                        mList.add(false);
                    }
                } else {
                    ivChoose.setSelected(true);
                    int size = mList.size();
                    mList.clear();
                    for (int i = 0; i < size; i++) {
                        mList.add(true);
                    }
                }
                adapter.notifyDataSetChanged();
                break;
            case R.id.tv_pay://付款
                Toast.makeText(getActivity(), "付款", Toast.LENGTH_SHORT).show();
                break;
        }
    }

}
